//
//  KSToolBarView.swift
//  AlertView
//
//  Created by Qiuxk
// on 2016/11/4.
//  Copyright © 2016年 scale. All rights reserved.
//
//

import UIKit

open class KSToolBarView: UIView {

    open var doneAction: (() -> Void)?
    open var cancelAction: (() -> Void)?
    
    // 把取消变成全部，个别需求需要这样做
      open func setCancelTitleToAll() {
          cancleBtn.setTitle("全部", for: .normal)
      }

    open var title = "请选择" {
        didSet {
            titleLabel.text = title
        }
    }

    // 分割线
    fileprivate lazy var contentView: UIView = {
        let content = UIView()
        content.backgroundColor = UIColor.white
        return content
    }()

    // 文本框
    fileprivate lazy var titleLabel: UILabel = {
        let label :UILabel = UILabel()
        label.textColor = UIColor.black
        label.textAlignment = .center
        return label
    }()

    // 取消按钮
    fileprivate lazy var cancleBtn: UIButton = {
        let btn:UIButton = UIButton()
        btn.setTitle("取消", for: UIControl.State())
        btn.setTitleColor(UIColor.black, for: UIControl.State())
        return btn
    }()

    // 确定按钮
    fileprivate lazy var doneBtn: UIButton = {
        let donebtn:UIButton = UIButton()
        donebtn.setTitle("确定", for: UIControl.State())
        donebtn.setTitleColor(UIColor.black, for: UIControl.State())
        return donebtn
    }()

    override public init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    fileprivate func setup() {
        backgroundColor = UIColor.lightText

        addSubview(contentView)
        contentView.addSubview(cancleBtn)
        contentView.addSubview(doneBtn)
        contentView.addSubview(titleLabel)

        doneBtn.addTarget(self, action: #selector(KSToolBarView.doneBtnOnClick), for: .touchUpInside)
        cancleBtn.addTarget(self, action: #selector(KSToolBarView.cancelBtnOnClick), for: .touchUpInside)
    }

    @objc fileprivate func doneBtnOnClick() {
        if let doneAction = self.doneAction {
            doneAction()
        }
    }

    @objc fileprivate func cancelBtnOnClick() {
        if let cancelAction = self.cancelAction {
            cancelAction()
        }
    }

    open override func layoutSubviews() {
        super.layoutSubviews()

        let margin: CGFloat = 15.0
        let height = bounds.size.height
        let width = bounds.size.width

        let contentHeight = height - 2.0
        contentView.frame = CGRect(x: 0.0, y: 1.0, width: width, height: contentHeight)

        let btnWidth = contentHeight
        cancleBtn.frame = CGRect(x: margin, y: 0.0, width: btnWidth, height: btnWidth)
        doneBtn.frame = CGRect(x: width - btnWidth - margin, y: 0.0, width: btnWidth, height: btnWidth)

        let titleX = cancleBtn.frame.maxX + margin
        let titleW = width - titleX - btnWidth - margin
        titleLabel.frame = CGRect(x: titleX, y: 0.0, width: titleW, height: btnWidth)
    }

}
